package com.woniuxy.education.service.impl;

import com.woniuxy.education.entity.Perm;
import com.woniuxy.education.entity.Student;
import com.woniuxy.education.entity.User;
import com.woniuxy.education.mapper.UserMapper;
import com.woniuxy.education.service.UserService;
import com.woniuxy.education.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     *
     * @param account
     * @return 在user总表中查询有没有角色
     */
    @Override
    public User findUserByAccont(String account) {
        return userMapper.findUserByAccont(account);
    }

    /**
     *
     * @param id
     * @return 根据userid查询具体的角色
     */
    @Override
    public Student findStudent(int id) {
        return userMapper.findStudent(id);
    }

    @Override
    public User findUserAndRolesByAccont(String account) {
        return userMapper.findUserAndRolesByAccont(account);
    }

    @Override
    public List<Perm> queryAllMenu(int uid) {
        //查出所有的rid
        List<Integer> listrid=userMapper.findByRid(uid);
        //这个保存所有的权限
        List listAllPerms=new ArrayList();
        //查出所有的pid
        List<Integer> listPid=new LinkedList<>();
        //查出当前pid下的id
        List<Integer> list=new ArrayList();
        List listPremsPid=new LinkedList<>();
        for (int i=0;i<listrid.size();i++){
            listPid=userMapper.findBypid(listrid.get(i));
            for (int j=0;j<listPid.size();j++){
                list=userMapper.findPermsid(listPid.get(j));
                for(int p=0;p<list.size();p++){
                    //这里需要重新new,不然会覆盖之前添加的
                    Perm prems=new Perm();
                    prems=findPerms(list.get(p),prems);
                    listAllPerms.add(prems);
                }
            }
        }

        System.out.println(listAllPerms);
        return  listAllPerms;
    }

    //这个findperms里面传的id就是主键id
       Perm findPerms(int id,Perm perm){
           perm.setChildren(new ArrayList<>());
        //通过pid查询出id.....Select("select id,name,pid from perms where pid = #{pid}")
           Perm permone=userMapper.findPerm(id);
           List<Integer> listPids=userMapper.findAllPermsPid(id);
           if(listPids.size()!=0){
               for(int i=0;i<listPids.size();i++){
                   perm.setMenuname(permone.getMenuname());
                   perm.setMenuid(permone.getMenuid());
                   perm.setPid(permone.getPid());
                   perm.setUrl(permone.getUrl());
                   perm.setIcon(permone.getIcon());
                   perm.getChildren().add(findPerms(listPids.get(i),new Perm()));
               }
           }else {
               return permone;
           }
        return perm;
    }

    @Override
    public boolean changephone(int id, int type_id, String head_picture) {
        //如果type_id等于0,则根据id修改学生的头像
        if(type_id==0){
            return userMapper.changeStuPhone(id,head_picture);
        }
        return false;
    }
}